package com.xsh.gls.controller;

import com.xsh.gls.pojo.Order;
import com.xsh.gls.pojo.Transfer;
import com.xsh.gls.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.UUID;

@RestController
@RequestMapping("/declare")
public class DeclareController {
    @Autowired
    private OrderService orderService;


    @RequestMapping(value = "/AddInsure")
    public Transfer AddInsure(@RequestParam(value = "declareImg", required = false) MultipartFile attachs,Integer ordid) {
        String logoPicPath = null;// LOGO图片url路径
        String logoLocPath = null;// LOGO图片的服务器存储路径
        Transfer transfer = new Transfer();

        //判断上传文件是否为空
        if (attachs!=null) {
            //原有图片名字
            String oldFileName = attachs.getOriginalFilename();
            //后缀
            String prefix = oldFileName.substring(oldFileName.lastIndexOf(".") + 1);
            //存储路径
            String path = "D:\\nginx-1.14.2\\html\\images";
            if (attachs.getSize() > 500000) {
                transfer.setMessage("size");
                return transfer;
            } else if (prefix.equalsIgnoreCase("jpg")
                    || prefix.equalsIgnoreCase("jpeg")
                    || prefix.equalsIgnoreCase("png")
                    || prefix.equalsIgnoreCase("pneg")) {
                //新名字
                String newFileName = UUID.randomUUID().toString()
                        .replace("-", "")
                        + "." + prefix;
                File file = new File(path, newFileName);
                try {
                    attachs.transferTo(file);
                } catch (Exception e) {
                    e.printStackTrace();
                    transfer.setMessage("fail");
                    return transfer;
                }
                logoPicPath = "images" + File.separator + newFileName;
                logoLocPath = path + File.separator + newFileName;
                Order order=new Order();
                order.setId(ordid);
                order.setInsureLocPath(logoLocPath);
                order.setInsurePicPath(logoPicPath);
                try {
                    if(orderService.updateInsureImgById(order)){
                        Order order2=orderService.queryOrderById(ordid);
                        if (order2.getShippingType()==1){
                            transfer.setMessage("toShip");
                        }else {
                            transfer.setMessage("toAir");
                        }
                        return transfer;
                    }else{
                        transfer.setMessage("fail");
                        return transfer;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }else{
                transfer.setMessage("type");
                return transfer;
            }
        }else{
            transfer.setMessage("kong");
            return transfer;
        }
        transfer.setMessage("fail");
        return transfer;

    }
    @RequestMapping(value = "/Adddeclare")
    public Transfer Adddeclare(@RequestParam(value = "declareImg", required = false) MultipartFile attachs,Integer ordid) {
        String logoPicPath = null;// LOGO图片url路径
        String logoLocPath = null;// LOGO图片的服务器存储路径
        Transfer transfer = new Transfer();

        //判断上传文件是否为空
        if (attachs!=null) {
            //原有图片名字
            String oldFileName = attachs.getOriginalFilename();
            //后缀
            String prefix = oldFileName.substring(oldFileName.lastIndexOf(".") + 1);
            //存储路径
            String path = "D:\\nginx-1.14.2\\html\\images";
            if (attachs.getSize() > 500000) {
                transfer.setMessage("size");
                return transfer;
            } else if (prefix.equalsIgnoreCase("jpg")
                    || prefix.equalsIgnoreCase("jpeg")
                    || prefix.equalsIgnoreCase("png")
                    || prefix.equalsIgnoreCase("pneg")) {
                //新名字
                String newFileName = UUID.randomUUID().toString()
                        .replace("-", "")
                        + "." + prefix;
                File file = new File(path, newFileName);
                try {
                    attachs.transferTo(file);
                } catch (Exception e) {
                    e.printStackTrace();
                    transfer.setMessage("fail");
                    return transfer;
                }
                logoPicPath = "images" + File.separator + newFileName;
                logoLocPath = path + File.separator + newFileName;
                Order order=new Order();
                order.setId(ordid);
                order.setDeclarelocpath(logoLocPath);
                order.setDeclarepicpath(logoPicPath);
                try {
                    if(orderService.updateDeclareImgById(order)){
                        Order order2=orderService.queryOrderById(ordid);
                        if (order2.getShippingType()==1){
                            transfer.setMessage("toShip");
                        }else {
                            transfer.setMessage("toAir");
                        }
                        return transfer;
                    }else{
                        transfer.setMessage("fail");
                        return transfer;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }else{
                transfer.setMessage("type");
                return transfer;
            }
        }else{
            transfer.setMessage("kong");
            return transfer;
        }
        transfer.setMessage("fail");
        return transfer;

    }
}
